From a1784a834496722b1249f490573f14355c06dc2f Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sat, 12 Jul 2014 16:02:38 +1000 Subject: [PATCH] Minor refactorings. There was some unnecessary duplication and allocation. --- src/cargo/util/toml.rs | 55 ++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 5a0f35e7f..44b40595f 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -30,6 +30,21 @@ impl Layout { } } +fn try_add_file(files: &mut Vec, root: &Path, dir: &str) { + let p = root.join(dir); + if p.exists() { + files.push(p); + } +} +fn try_add_files(files: &mut Vec, root: &Path, dir: &str) { + match fs::readdir(&root.join(dir)) { + Ok(new) => { + files.extend(new.move_iter().filter(|f| f.extension_str() == Some("rs"))) + } + Err(_) => {/* just don't add anything if the directory doesn't exist, etc. */} + } +} + pub fn project_layout(root: &Path) -> Layout { let mut lib = None; let mut bins = vec!(); @@ -40,32 +55,14 @@ pub fn project_layout(root: &Path) -> Layout { lib = Some(root.join("src/lib.rs")); } - if root.join("src/main.rs").exists() { - bins.push(root.join("src/main.rs")); - } - - let _ = fs::readdir(&root.join("src/bin")) - .map(|v| v.move_iter()) - .map(|i| i.filter(|f| f.extension_str() == Some("rs"))) - .map(|mut i| i.collect()) - .map(|found| bins.push_all_move(found)); + try_add_file(&mut bins, root, "src/main.rs"); + try_add_files(&mut bins, root, "src/bin"); - let _ = fs::readdir(&root.join("examples")) - .map(|v| v.move_iter()) - .map(|i| i.filter(|f| f.extension_str() == Some("rs"))) - .map(|mut i| i.collect()) - .map(|found| examples.push_all_move(found)); + try_add_files(&mut examples, root, "examples"); // support two styles of tests: src/test.rs or tests/*.rs - let _ = fs::readdir(&root.join("tests")) - .map(|v| v.move_iter()) - .map(|i| i.filter(|f| f.extension_str() == Some("rs"))) - .map(|mut i| i.collect()) - .map(|found| tests.push_all_move(found)); - - if root.join("src/test.rs").exists() { - tests.push(root.join("src/test.rs")); - } + try_add_file(&mut tests, root, "src/test.rs"); + try_add_files(&mut tests, root, "tests"); Layout { lib: lib, @@ -256,11 +253,9 @@ fn inferred_bin_targets(name: &str, layout: &Layout) -> Option> fn inferred_example_targets(layout: &Layout) -> Option> { Some(layout.examples.iter().filter_map(|ex| { - let name = ex.filestem_str().map(|f| f.to_string()); - - name.map(|name| { + ex.filestem_str().map(|name| { TomlTarget { - name: name, + name: name.to_string(), crate_type: None, path: Some(ex.display().to_string()), test: None, @@ -272,11 +267,9 @@ fn inferred_example_targets(layout: &Layout) -> Option> { fn inferred_test_targets(layout: &Layout) -> Option> { Some(layout.tests.iter().filter_map(|ex| { - let name = ex.filestem_str().map(|f| f.to_string()); - - name.map(|name| { + ex.filestem_str().map(|name| { TomlTarget { - name: name, + name: name.to_string(), crate_type: None, path: Some(ex.display().to_string()), test: None, -- 2.30.2